package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.request.CommodityRequestParams;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.CommercialTenant;
import com.market.entity.Commodity;
import com.market.service.CommodityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Resource
    CommodityService commodityService;

    /**
     * 新增商品
     *
     * @param params
     * @return
     * @throws ControllerException
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody CommodityRequestParams params) throws ControllerException {
        Commodity commodity = new Commodity();
        commodity.setCommodityClassesName(params.getCommodityClassesName());
        commodity.setCommodityName(params.getCommodityName());
        commodity.setCommodityBrandName(params.getCommodityBrandName());
        commodity.setCommodityUnitsName(params.getCommodityUnitsName());
        commodity.setRetailPrice(params.getRetailPrice());
        commodity.setMinSalePrice(params.getMinSalePrice());
        commodity.setMarketGuidePrice(params.getMarketGuidePrice());
        commodity.setCostReferencePrice(params.getCostReferencePrice());
        commodity.setDefaultPurchasePrice(params.getDefaultPurchasePrice());
        int i = commodityService.addCommodity(commodity);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 删除
     * @param commodityId
     * @return
     * @throws ControllerException
     */
    @PostMapping("/delete")
    public ResponseResult<Integer> delete(@RequestParam Integer commodityId)throws ControllerException{
        int i = commodityService.deleteCommdity(commodityId);
        return ResponseResult.success(ResponseCode.SUCCESS,i);
    }

    /**
     * 查询
     * @return
     * @throws ControllerException
     */
    @GetMapping("/find")
    public ResponseResult<List<Commodity>> find() throws ControllerException {
        List<Commodity> commodity = commodityService.findCommodity();
        return ResponseResult.success(ResponseCode.SUCCESS, commodity);
    }
}
